package com.cy.aircompressor.main.ui.other.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.api.RepositoryImpl;
import com.cy.aircompressor.entity.warning.WarningConfig;
import com.cy.aircompressor.entity.warning.WarningMsg;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.net.model.base.Resource;
import com.net.model.viewmodel.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OtherViewModel extends BaseViewModel<RepositoryImpl> {
    private MediatorLiveData<Resource<RowEntity<WarningMsg>>> queryWarningByPageObservable;
    private MediatorLiveData<Resource<RowEntity<WarningConfig>>> queryWarningConfigByPage;
    private MediatorLiveData<Resource<Object>> batchUpdateWarningConfig;
    private MediatorLiveData<Resource<Object>> updateStatus;

    public OtherViewModel(@NonNull Application application) {
        super(application);
        bindBaseModel(RepositoryImpl.get());
        queryWarningByPageObservable = new MediatorLiveData<>();
        queryWarningConfigByPage = new MediatorLiveData<>();
        batchUpdateWarningConfig = new MediatorLiveData<>();
        updateStatus = new MediatorLiveData<>();
    }

    public void queryWarningByPage(Integer pageNum, Integer pageSize, String compressorId) {
        queryWarningByPageObservable.addSource(baseModel.queryWarningByPage(pageNum, pageSize, compressorId), new Observer<Resource<RowEntity<WarningMsg>>>() {
            @Override
            public void onChanged(Resource<RowEntity<WarningMsg>> rowEntityResource) {
                queryWarningByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public void queryWarningConfigByPage(Integer pageNum, Integer pageSize, String compressorModel) {
        queryWarningConfigByPage.addSource(baseModel.queryWarningConfigByPage(pageNum, pageSize, compressorModel,
                null, null, null, null), new Observer<Resource<RowEntity<WarningConfig>>>() {
            @Override
            public void onChanged(Resource<RowEntity<WarningConfig>> rowEntityResource) {
                queryWarningConfigByPage.setValue(rowEntityResource);
            }
        });
    }

    public void batchUpdateWarningConfig(List<Object> parameters) {
        batchUpdateWarningConfig.addSource(baseModel.batchUpdateWarningConfig(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                batchUpdateWarningConfig.setValue(rowEntityResource);
            }
        });
    }
    public void updateStatus(String id) {
        updateStatus.addSource(baseModel.updateStatus(id), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                updateStatus.setValue(rowEntityResource);
            }
        });
    }

    public MediatorLiveData<Resource<RowEntity<WarningMsg>>> getQueryWarningByPageObservable() {
        return queryWarningByPageObservable;
    }

    public MediatorLiveData<Resource<RowEntity<WarningConfig>>> getQueryWarningConfigByPage() {
        return queryWarningConfigByPage;
    }

    public MediatorLiveData<Resource<Object>> getBatchUpdateWarningConfig() {
        return batchUpdateWarningConfig;
    }

    public MediatorLiveData<Resource<Object>> getUpdateStatus() {
        return updateStatus;
    }
}
